﻿package fwk.mediaplayer{
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.media.Video;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.NetStream;
	import flash.net.NetConnection;
	import flash.utils.Timer;
	import flash.events.MouseEvent;
    import flash.events.NetStatusEvent;
    import flash.events.SecurityErrorEvent;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.events.FullScreenEvent;
	import flash.display.StageDisplayState;
	
	/**
	* Lecteur de flv et mp3.
	* @version 1.0
	* @usage var myPlayer:PlayerFLVMP3 = new PlayerFLVMP3(player);
	* @usage myPlayer.load("test.flv");
	* @usage	
	* @usage myPlayer.addEventListener("onComplete", onComplete);
	* @usage myPlayer.addEventListener("onMetaData", onMetaData);
	* @usage myPlayer.addEventListener("onCuePoint", onCuePoint);
	* @usage myPlayer.addEventListener("onNetStatus", onNetStatus);
	* @usage	
	* @usage function onComplete(event:Event){
	* @usage	trace("lecture du média fini");
	* @usage }
	* @usage	
	* @usage function onCuePoint(event:Event){
	* @usage	trace("onCuePoint");
	* @usage	trace(myPlayer.infoCuePoint);
	* @usage }
	* @usage	
	* @usage function onMetaData(event:Event){
	* @usage	trace("onMetaData");
	* @usage	trace(myPlayer.infoMetaData);
	* @usage }
	* @usage function onNetStatus(event:Event){
	* @usage	trace("onNetStatus");
	* @usage	trace(myPlayer.infoNetStatus);
	* @usage }
	*/
	public class PlayerFLVMP3 extends Sprite{
	
		private var _mc:MovieClip;
		private var _clipParent:MovieClip;
		private var _clipProgress:MovieClip;
		private var _progressText;
		private var _downloadProgress:MovieClip;
		private var _progress:MovieClip;
		private var _clickBar:MovieClip;
		private var _butSlider:MovieClip;
		private var _controleVolume:MovieClip;
		private var _clipChargement:MovieClip;
		
		private var _widthProgress:Number;
		private var _bufferTime:Number;
		private var _volume:Number;
		private var _timerProgress:Timer;
		private var _mediaDuration:Number;
		private var _mediaLoaded:Boolean;
		
		private var _isPause:Boolean;
		private var _isSound:Boolean;
		private var _isStop:Boolean;
		public var _isPlaying:Boolean;
		
		private var _sliderVolumeIsDrag:Boolean;
		private var _sliderIsDrag:Boolean;
		private var _isMP3:Boolean;
		private var _autoplay:Boolean;
		private var _type:String = "-1";
		
		// pour le son
		private var _sound:Sound;
		private var _soundChannel:SoundChannel;
		private var _soundURL:String = "-1";
		private var _soundPosition:Number;
		
		/// pour les flv
		private var _video_nc:NetConnection;
		private var _video_ns:NetStream;
		private var _flvURL:String = "-1";
		private var _screen:Video;
		
		private var _screenWidth:Number;
		private var _screenHeight:Number;
		
		private var _videoWidth:Number;
		private var _videoHeight:Number;
		
		private var _containerWidth:Number;
		private var _containerHeight:Number;
		
		private var _screenX:int;
		private var _screenY:int;
		
		/// info utiles en callback
		/**
		* Pour les FLV, l'object metadata. Renseigné une fois l'evenement onMetaData déclenché.
		*/
		public var infoMetaData:Object;
		/**
		* Pour les FLV, l'object CuePoint. Renseigné une fois l'evenement onCuePoint déclenché.
		*/
		public var infoCuePoint:Object;
		/**
		* Pour les FLV, l'object NetStatus. Renseigné une fois l'evenement onNetStatus déclenché.
		*/
		public var infoNetStatus:Object;
		
		//-------- Constructeur
		//--------------------------------------------------------------------------
		/**
		* Contructeur prenant en paramètre le clip de l'interface graphique du player
		* @param	playerMC clip de l'interface graphique du player
		*/
		function PlayerFLVMP3(playerMC:MovieClip) {
			
			this._mc = playerMC;
			this._clipParent = playerMC.parent as MovieClip;
			this._screen = this._mc.screen;
			this._screen.visible = false;
			this._screen.smoothing = true;
			this._clipChargement = this._mc.clip_chargement;

			this._controleVolume = this._mc.controles.volume;
			this._clipProgress = this._mc.controles.clip_progress;
			this._progressText = this._mc.controles.time.tTexte;
			
			this._downloadProgress = this._clipProgress.downloadProgress;
			this._progress = this._clipProgress.progress;
			this._clickBar = this._clipProgress.clickBar;
			this._butSlider = this._clipProgress.slider;
			
			this._mc.visible = true;
			if(this._butSlider) this._butSlider.x = 0;
			this._sliderIsDrag = false;
			this._widthProgress = this._clipProgress.fond.width;
			this._downloadProgress.width = 0;
			this._progress.width = 0;
			this._clickBar.width = 0;
			this._mediaLoaded = false;
			this._clipChargement.visible = false;
			this._isPause = true;
			this._isSound = true;
			this._isStop = true;
			this._screenWidth 	= this._screen.width;
			this._screenHeight 	= this._screen.height;
			_containerWidth		= _screenWidth;
			_containerHeight	= _screenHeight;
			
			if (_mc.fond != null) {
				_mc.fond.width			= _containerWidth;
				_mc.fond.height			= _containerHeight;
			}	
			
			this._screenX = int(this._screen.x);
			this._screenY = int(this._screen.y);
			
			// config default
			this._volume = 0.7;
			this._bufferTime = 2;
			this._autoplay = true;
			
			enableButs(false);
			this.initClickBut();

		}
		public function get clip():MovieClip
		{
			return _mc;
		}
		public function get containerWidth():Number
		{
			return _containerWidth;
		}
		public function get containerHeight():Number
		{
			return _containerHeight;
		}
		/**
		* Déclenche la lecture du média.
		*/
		public function play(){
			this.playMedia();
			_isPlaying = true;
		}
		/**
		* Stop le media en cours.
		*/
		public function stop(){
			this.stopMedia();
			_isPlaying = false;
		}
		/**
		* Pause le media en cours.
		*/
		public function pause(){
			this.pauseMedia();
			_isPlaying = false;
		}
		/**
		* True si lecture automatique ou False si lecture au clic. True par défaut.
		* @param	value True si lecture automatique ou False si lecture au clic. True par défaut.
		*/
		public function set autoplay(value:Boolean){
			_autoplay = value;
		}
		/**
		* Spécifie la taille du buffer en secondes. Par défaut égal à 2
		* @param	value taille du buffer en secondes. Number.
		*/
		public function set bufferTime(value:Number){
			_bufferTime = value;
		}
		/**
		* Renvoie la taille actuelle du buffer en secondes.
		* @return bufferTime, taille actuelle du buffer en secondes.
		*/
		public function get bufferTime():Number{
			return _bufferTime;
		}
		/**
		* Spécifie le type de media. Egal à "mp3" ou "flv". Par défaut égal à "-1". Cela veut dire que la player se fie à l'extension du fichier passer par la méthode Load
		* @param	value  String égal à "mp3" ou "flv"
		*/
		public function set typeMedia(value:String){
			_type = value;
		}
		
		public function set volume(value:Number){
			setVolume(value);
		}
		public function get volume():Number {
			
			var transform:SoundTransform;
			
		    if (!this._isMP3){
				if(this._video_ns != null) {
					transform = _video_ns.soundTransform;
				}
			}else{
				if(this._soundChannel != null){
					transform = _soundChannel.soundTransform;
				}
			} 
			
			return transform.volume;
		}
		
		
		/**
		* initialise le player.
		*/
		public function init(){
			enableButs(false);
			
			_soundURL 	= "-1";
			_flvURL 	= "-1";
			_type		= "-1";
			stopMedia();
			if(this._video_nc) this._video_nc.close();
		}
		//-------- load media
		//--------------------------------------------------------------------------
		/**
		* Charge un media.
		* @param	url Fichier media à lire
		*/
		public function load(url:String){
			enableButs(true);
			
			// on clear le timer precedent
			stopTimerProgress();

			this.stopMedia();
			this._mc.visible = true;

			if(_type == "-1"){
				var extType:Array = url.split(".");
				_type = extType[extType.length-1];
			}
			// on regarde si flv ou mp3
			if(_type == "flv"){
				this._isMP3 = false;
				this._flvURL = url;
			}else{
				this._isMP3 = true;
				this._soundURL = url;
				this._screen.visible = false;
			}
			
			this._isPause = true;
			this._isSound = true;
			this._isStop = true;
			
			this.updateTimerText(0);
			
			if(_autoplay){
				this.playMedia();
			}
		}
		//--------------------------------------------------------------------------
		public function setScaleScreen(w:Number,h:Number){
			this._screenWidth 	= w;
			this._screenHeight 	= h;
		}
		//-------- click boutons
		//--------------------------------------------------------------------------
		private function initClickBut(){
						
			
			//stage fullscreen
			_mc.stage.addEventListener(FullScreenEvent.FULL_SCREEN, onFullScreenChange);
			
			
			/// click bar et but slider
			this._clickBar.buttonMode = true;
			this._clickBar.addEventListener(MouseEvent.MOUSE_DOWN, clickBarOnPress);			
			if(this._butSlider){
				this._butSlider.buttonMode = true;
				this._butSlider.addEventListener(MouseEvent.MOUSE_DOWN, clickBarOnPress);
				initRollOver(this._butSlider);
			}
				function clickBarOnPress(evt:MouseEvent){
					if(!_isStop){
						sliderDrag();
						clickBar();
						_clickBar.stage.addEventListener(MouseEvent.MOUSE_UP, clickBarOnRelease);
					}
				}
				function clickBarOnRelease(evt:MouseEvent){
					_clickBar.stage.removeEventListener(MouseEvent.MOUSE_UP, clickBarOnRelease);
					sliderUnDrag();
				}
			
			
			/// boutons son
			if(this._mc.controles.butSon){
				this._mc.controles.butSon.addEventListener(MouseEvent.MOUSE_DOWN, butSonOnPress);
				this._mc.controles.butSon.buttonMode = true;
				function butSonOnPress(evt:MouseEvent){
					if(_isSound){
						sonOff();
					}else{
						sonOn();
					}
				}
			}
			
			/// boutons play
			if(this._mc.controles.butPlay){
				this._mc.controles.butPlay.buttonMode = true;
				this._mc.controles.butPlay.addEventListener(MouseEvent.MOUSE_DOWN, butPlayOnPress);
				initRollOver(this._mc.controles.butPlay);
					function butPlayOnPress(evt:MouseEvent){
						if(_isPause){
							playMedia();
						}else{
							pauseMedia();
						}
					}
			}
				
			/// boutons stop
			if(this._mc.controles.butStop){
				this._mc.controles.butStop.buttonMode = true;
				this._mc.controles.butStop.addEventListener(MouseEvent.MOUSE_DOWN, butStopOnPress);
				initRollOver(this._mc.controles.butStop);
					function butStopOnPress(evt:MouseEvent){
						stopMedia();
					}
				
			}
			
			//bt fullscreen
			if(_mc.controles.btFullscreen){
				_mc.controles.btFullscreen.buttonMode = true;
				_mc.controles.btFullscreen.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownBtFullScreen);
				initRollOver(_mc.controles.btFullscreen);
				
			}
				
			/// controle volume
			if(this._controleVolume){
				this._controleVolume.clickBar.buttonMode = true;
				this._controleVolume.clickBar.addEventListener(MouseEvent.MOUSE_DOWN, controleVolumeOnPress);
				if(this._controleVolume.slider){
					this._controleVolume.slider.buttonMode = true;
					this._controleVolume.slider.addEventListener(MouseEvent.MOUSE_DOWN, controleVolumeOnPress);
				}

					function controleVolumeOnPress(evt:MouseEvent){
						if(!_isStop){
							_sliderVolumeIsDrag = true;
							_controleVolume.clickBar.stage.addEventListener(MouseEvent.MOUSE_UP, controleVolumeOnRelease);
						}
					}
					function controleVolumeOnRelease(evt:MouseEvent){
						_controleVolume.clickBar.stage.removeEventListener(MouseEvent.MOUSE_UP, controleVolumeOnRelease);
						_sliderVolumeIsDrag = false;
					}
			}
		}
		//--------------------------------------------------------------------------
		private function enableButs(flag:Boolean){
				this._mc.controles.enebled = flag;
				this._mc.controles.mouseEnabled = flag;
				this._mc.controles.mouseChildren = flag;
		}
		//--------------------------------------------------------------------------
		private function initRollOver(clip:MovieClip){
			clip.addEventListener(MouseEvent.MOUSE_OVER, onRollOverClip);
			clip.addEventListener(MouseEvent.MOUSE_OUT, onRollOutClip);
				function onRollOverClip(event:MouseEvent){
					clip.gotoAndPlay("on");
				}
				function onRollOutClip(event:MouseEvent){
					clip.gotoAndPlay("off");
				}
		}
		//--------------------------------------------------------------------------
		private function initFlv(){
			if(_flvURL != "-1"){
				
				this._mediaLoaded = false;
				this._mc.controles.butPlay.clip.gotoAndStop(2);
				this._clipChargement.visible = true;
				
				//NetConnection
				this._video_nc = new NetConnection();
				this._video_nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
				this._video_nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				this._video_nc.connect(null);
			}

		}
		// Net Status
		private function netStatusHandler(event:NetStatusEvent) {
			
			var code:String = event.info.code;
			switch (code) {
				case "NetStream.Buffer.Full":
					_clipChargement.visible = false;
					break;
				case "NetStream.Buffer.Flush":
					break;
				case "NetStream.Buffer.Empty":
					break;
				case "NetStream.Play.Stop":
					//verifie si fin vidéo
					trace((_video_ns.time * 1000));
					trace(_mediaDuration);
					if ((_video_ns.time * 1000) >= (_mediaDuration - 700) && !isNaN(_mediaDuration))
					{
						endMedia();
					}
					break;
				case "NetConnection.Connect.Success":
					connectStream();
					break;
				case "NetStream.Play.StreamNotFound":
					trace("XXXXXX -- Stream not found");
					break;
			}
			
			infoNetStatus = event.info;
			dispatchEvent(new Event("onNetStatus"));
		}
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		//--------------------------------------------------------------------------
		private function connectStream(){
			trace("connectStream " + this);
			
			//NetStream
			_video_ns = new NetStream(_video_nc);
			_video_ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			_video_ns.client = this;
			_video_ns.bufferTime = _bufferTime;
			_screen.attachNetStream(_video_ns);
			
			var cheminvideo:URLRequest = new URLRequest(_flvURL);
			_video_ns.play(cheminvideo.url);
			
			setVolume(_volume);
		}
		//--------------------------------------------------------------------------
		public function onMetaData(info:Object):void {
			trace("info.duration " + info.duration);
			 _mediaDuration = Math.round(info.duration * 1000);
			 
			 if(info.width)
			 _videoWidth = info.width;
			 
			 if(info.height)
			 _videoHeight = info.height;
			 
			 
			 scaleVideoWithRatio();
			 
			 
			infoMetaData = info;
			dispatchEvent(new Event("onMetaData"));

			this._screen.visible = true;
			
			this.startTimerProgress();
		}
 
		//--------------------------------------------------------------------------
		public function onXMPData(e:Object):void
		{
			trace("onXMPData");
		}

		//--------------------------------------------------------------------------
		public function onLastSecond(e:Object):void
		{
			trace("onLastSecond");
		}

		
		
		//-----------------------.------------------------------------------------
		private function onMouseDownBtFullScreen(event:MouseEvent)
		{
			  try{
				  if(event.currentTarget.stage.displayState  != StageDisplayState.FULL_SCREEN){
						event.currentTarget.stage.displayState = StageDisplayState.FULL_SCREEN;
				  }
				  else
				  {
						event.currentTarget.stage.displayState = StageDisplayState.NORMAL;
				  }
			  }catch (e) {
				  
				  
			  }

		}
		//-----------------------.------------------------------------------------
		private function onFullScreenChange(e:FullScreenEvent):void {

			if (e.fullScreen)
			{
				_mc.stage.addChild(_mc);
				_containerWidth		= _mc.stage.stageWidth;
				_containerHeight	= _mc.stage.stageHeight;
				scaleVideoWithRatio();

			} else {
				_clipParent.addChild(_mc);
				_containerWidth		= _screenWidth;
				_containerHeight	= _screenHeight;
				scaleVideoWithRatio();

			}
			
			if (_mc.fond != null) {
				_mc.fond.width			= _containerWidth;
				_mc.fond.height			= _containerHeight;
			}	
			
		}
		
		//pour recentrage video si pas exactement la même taille du conteneur video
		private function scaleVideoWithRatio()
		{
				if(_videoWidth != -1 && _videoHeight != -1)
				{
					 var ratioScreen:Number = _containerWidth / _containerHeight;
					 var ratio:Number = _videoWidth / _videoHeight;
					 
					 if (ratio == ratioScreen)
					 {
						_screen.width = _containerWidth;
						_screen.height = _containerHeight;
					 }else
					 {
						 if (ratio > ratioScreen)
						 {
							_screen.width = _containerWidth;
							_screen.height = _containerWidth/ratio;
						 } else
						 {
							_screen.height = _containerHeight;
							_screen.width = ratio*_containerHeight;
						 }
					 }
					 

					_screen.x =  (_containerWidth - _screen.width) / 2;
					_screen.y =  (_containerHeight - _screen.height) / 2;
					
					
				}
		}
		
		
		public function onCuePoint(info:Object):void {
			infoCuePoint = info;
			dispatchEvent(new Event("onCuePoint"));
		}
		public function onPlayStatus(info:Object):void {
			trace(" onPlayStatus ");
			
			var code:String = info.code;
            switch (code) {
                case "NetStream.Play.Complete":
                    //endMedia();
                    break;
            }	
		}
		//--------------------------------------------------------------------------
		private function initMp3(){
			if(_soundURL != "-1"){
				_mediaLoaded = false;
				_mc.visible = true;
				_screen.visible = false;
				_mc.controles.butPlay.clip.gotoAndStop(2);
				_clipChargement.visible = true;	
				
				var urlSon:URLRequest = new URLRequest(this._soundURL);
				_sound = new Sound();
				_sound.addEventListener(Event.COMPLETE, completeLoadSound);
				_sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				_sound.addEventListener(ProgressEvent.PROGRESS, progressLoadSound);
				_sound.load(urlSon);
				
				_soundChannel = this._sound.play();
				_soundChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
				
				startTimerProgress();
				

				
				setVolume(_volume);
			}
		}
		//-----------------------------------------------------------------------
		private function ioErrorHandler(event:IOErrorEvent){
            _sound.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            _sound.removeEventListener(ProgressEvent.PROGRESS, progressLoadSound);
            _sound.removeEventListener(Event.COMPLETE, completeLoadSound);
		}
		//-----------------------------------------------------------------------
		private function progressLoadSound(event:ProgressEvent){
			 var pctLoaded:Number = 0;
			 pctLoaded = Math.round(_sound.bytesLoaded/_sound.bytesTotal);
				 _downloadProgress.width = (pctLoaded)*_widthProgress;
				 _clickBar.width = _downloadProgress.width;
				 _mediaDuration = Math.floor(((_sound.length*_sound.bytesTotal )/_sound.bytesLoaded));

			 if(_clipChargement.visible && _sound.length >= (_bufferTime*1000)){
				_clipChargement.visible = false;
			 }
		}
		//-----------------------------------------------------------------------
		private function completeLoadSound(event:Event){
			 _mediaDuration = Math.floor(_sound.length);
			 _downloadProgress.width = _widthProgress;
			 _clickBar.width = _downloadProgress.width;
			 _mediaLoaded = true;
			 dispatchEvent(new Event("onLoadComplete"));
            _sound.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            _sound.removeEventListener(ProgressEvent.PROGRESS, progressLoadSound);
            _sound.removeEventListener(Event.COMPLETE, completeLoadSound);
		}
		//-----------------------------------------------------------------------
		private function soundCompleteHandler(event:Event){
			trace("soundCompleteHandler");
			endMedia();
		}
		//-----------------------------------------------------------------------
		private function startTimerProgress(){
			this.stopTimerProgress();
			trace("startTimerProgress");
			
			if(!this._isMP3){				
				_timerProgress = new Timer(1000/30, 0);
				_timerProgress.addEventListener("timer", progressMediaFLV);
				_timerProgress.start();
			}else{
				_timerProgress = new Timer(1000/30, 0);
				_timerProgress.addEventListener("timer", progressMediaMP3);
				_timerProgress.start();
			}
		}
		//-----------------------------------------------------------------------
		private function stopTimerProgress(){
			
			if(_timerProgress != null && _timerProgress.running){
				_timerProgress.stop();
				trace("stopTimerProgress");
			}
		}
		//-----------------------------------------------------------------------
		private function progressMediaFLV(event:Event){	
			if(!_isStop){
				_widthProgress = _clipProgress.fond.width;
				
				// download progress
				if(!_mediaLoaded){
					 var pctLoaded:Number = Math.round(_video_ns.bytesLoaded/_video_ns.bytesTotal*100);
					 trace(pctLoaded);
					 if (pctLoaded < 100) {
						 _downloadProgress.width = (pctLoaded/100)*_widthProgress;
						 _clickBar.width = _downloadProgress.width;
					 }else{
						 dispatchEvent(new Event("onLoadComplete"));
						 _downloadProgress.width = _widthProgress;
						 _clickBar.width = _downloadProgress.width;
						 _mediaLoaded = true;
					 }
				}else{
					 _downloadProgress.width = _widthProgress;
					 _clickBar.width = _downloadProgress.width;
				}
				
				// progress lecture media
				if (_sliderIsDrag) {
					dragSlider();
				}else{
					var position = Math.round(_video_ns.time*1000);
					var pourcentage = position/_mediaDuration;
					updateSliderPosition(pourcentage);
					updateTimerText(position);
				}
			}
			
			// controle volume
			if(_sliderVolumeIsDrag){
				updateVolumeSlider();
			}
			
		}
		//-----------------------------------------------------------------------
		private function progressMediaMP3(event:Event){	
			_widthProgress = _clipProgress.fond.width;
			
			// progress lecture media
			if (_sliderIsDrag) {
				dragSlider();
			}else{
				var position = this._soundChannel.position;
				_soundPosition =  Math.round(position);
				var pourcentage = (this._soundChannel.position)/_mediaDuration;
				updateSliderPosition(pourcentage);
				updateTimerText(position);
			}
			
			// controle volume
			if(_sliderVolumeIsDrag){
				updateVolumeSlider();
			}
			
		}
		//-----------------------------------------------------------------------
		private function dragSlider(){
				var pos = this._clipProgress.mouseX;
				var pourcentage = pos/this._widthProgress;
				// limite du drag
				if(pos>this._clickBar.width) pourcentage = this._clickBar.width/this._widthProgress;
				if(pos<0) pourcentage = 0;
							
				var position:Number;
				if(!this._isMP3){
					position = Math.round( this._video_ns.time*1000);
				}else{
					position = this._mediaDuration*pourcentage;
				}

				this.setMediaPosition(pourcentage);
				this.updateSliderPosition(pourcentage);
				this.updateTimerText(position);
		}
		
		//-----------------------------------------------------------------------
		private function updateSliderPosition(pourcentage)
		{
			if(this._butSlider) this._butSlider.x = (this._widthProgress - 3)*pourcentage;
			this._progress.width = (this._widthProgress - 3)*pourcentage;
		}
		//-----------------------------------------------------------------------
		private function updateTimerText(position:Number)
		{
			this._progressText.text = this._makeTime(position);
		}
		//-----------------------------------------------------------------------
		private function clickBar()
		{ 
		   var pourcentage = this._clipProgress.mouseX/this._widthProgress;
		   this.setMediaPosition(pourcentage);
		}
		//-----------------------------------------------------------------------
		private function sliderDrag()
		{
			this._sliderIsDrag = true;
			if(!this._isPause){ this.pauseMedia();
				this._isPause = false;
			}
		}
		//-----------------------------------------------------------------------
		public function sliderUnDrag()
		{ 
		   this._sliderIsDrag = false;
		   if(!this._isPause) this.playMedia();
		}
		//-----------------------------------------------------------------------
		private function setMediaPosition(pourcentage)
		{
			
		   var msecondes = this._mediaDuration*pourcentage; 

		   if (!this._isMP3) { 
			   this._video_ns.seek(Math.floor(msecondes/1000));
		   }else{
			   this._soundPosition = msecondes;
		   }
		}
		//-----------------------------------------------------------------------
		private function playMedia()
		{
		   
		   if(this._isStop){
				if (!this._isMP3) initFlv();
				else initMp3();
		   }else{
			   if (!this._isMP3) { 
				   if(this._video_ns != null) this._video_ns.resume();
			   }else{
				   if(this._soundChannel != null) this._soundChannel = this._sound.play(this._soundPosition);
			   }
		   }
		   this._isStop = false;
		   this._isPause = false;
		   	_isPlaying = true;
		   this._mc.controles.butPlay.clip.gotoAndStop(2);
		}
		//-----------------------------------------------------------------------
		private function pauseMedia()
		{
		   if (!this._isMP3) { 
			   if(this._video_ns != null) this._video_ns.pause();
		   }else{
			   if(this._soundChannel != null) this._soundChannel.stop();
		   }
		   this._mc.controles.butPlay.clip.gotoAndStop(1);
		   this._isPause = true;
		   
		}
		//-----------------------------------------------------------------------
		private function stopMedia()
		{
			if(!this._isStop){
				   if (!this._isMP3) { 
					    if(this._video_ns != null) this._video_ns.soundTransform.volume = 0;
					    if(this._video_ns != null){
							this._screen.clear();
							this._video_ns.close();
						}
				   }else{
					   if(this._soundChannel != null)  this._soundChannel.stop();
				   }
				   this._mc.controles.butPlay.clip.gotoAndStop(1);
				   this._isPause = true;
				   this.updateSliderPosition(0);
				   this.updateTimerText(0);
				   this._isStop = true;
				   this._clipChargement.visible = false;
			}
			this.stopTimerProgress();
		}
		private function endMedia() {
			
			
			trace("media end");
			stopMedia();
			dispatchEvent(new Event("onComplete"));
		}
		//-----------------------------------------------------------------------
		private function sonOff(){
			var transform:SoundTransform;
		    if (!this._isMP3) {
				if(this._video_ns != null) {
					transform = _video_ns.soundTransform;
					transform.volume = 0;
					this._video_ns.soundTransform = transform;
				}
			}else{
				if(this._soundChannel != null){
					transform = _soundChannel.soundTransform;
					transform.volume = 0;
					_soundChannel.soundTransform = transform;
				}
			}  
		   if(this._controleVolume){
			   this._controleVolume.maskVolume.width = 0;
			   if(this._controleVolume.slider) this._controleVolume.slider.x = this._controleVolume.maskVolume.width;
		   }
		   
		   this._isSound = false;
		   this._mc.controles.butSon.gotoAndStop(2);
		}
		//-----------------------------------------------------------------------
		private function sonOn(){
		   setVolume(this._volume);
		   this._isSound = true;
		   this._mc.controles.butSon.gotoAndStop(1);
		}
		//-----------------------------------------------------------------------
		private function setVolume(valeur:Number){

			this._volume = valeur;
			// limite
			if(valeur > 1) this._volume = 1;
			if(valeur < 0) this._volume = 0;
			
			if(this._mc.controles.butSon){
				if (this._volume > 0) {
					this._mc.controles.butSon.gotoAndStop(1);
					this._isSound = true;
				}
				else {
					this._mc.controles.butSon.gotoAndStop(2);
					this._isSound = false;
				}
			}

			
			var transform:SoundTransform;
		    if (!this._isMP3) {
				if(this._video_ns != null) {
					transform = _video_ns.soundTransform;
					transform.volume = this._volume;
					this._video_ns.soundTransform = transform;
				}
			}else{
				if(this._soundChannel != null){
					trace("volume : " + this._volume);
					transform = _soundChannel.soundTransform;
					transform.volume = this._volume;
					_soundChannel.soundTransform = transform;
				}
			}  
		   if(this._controleVolume){
			   this._controleVolume.maskVolume.width = this._volume*this._controleVolume.fond.width;
			   if(this._controleVolume.slider) this._controleVolume.slider.x = this._controleVolume.maskVolume.width;
		   }
		}
		//-----------------------.------------------------------------------------
		private function updateVolumeSlider(){
			this.setVolume((this._controleVolume.mouseX/this._controleVolume.fond.width));
		}
		//-----------------------------------------------------------------------
		private function _makeTime( totalMilliseconds:Number ):String 
		{       
			var mins = Math.floor((totalMilliseconds/1000) / 60); 
			var secs = Math.floor((totalMilliseconds/1000) % 60);
			
			var str = ""; 
			if ( mins == 0 ) 
				str += "00:" ;
			if ( mins < 10 && mins > 0 ) 
				str += "0" ;
			if ( mins > 0 ) 
				str += mins.toString () + ":" ;
			if ( secs < 10 ) 
				str += "0" ; 
				
			str += secs ; 
			
			return str ; 
		}

		//-----------------------------------------------------------------------
		public function gotoMilliseconds(millisecond:Number){

		   if (!this._isMP3) { 
			   this._video_ns.seek(Math.floor(millisecond/1000));
		   }
			
		}	
		
	}
}